<template>
  <div>
    <div v-if="topics.length">
      <div class="content" v-for="item in topics" :key="item.topic_id">
        <!-- 标题 -->
        <el-row>
          <a :href="'/topic/' + item.topic_id + ''">
            <h1>{{ item.topic_title }}</h1>
          </a>
        </el-row>

        <el-row>
          <!-- 图片 -->
          <el-col :span="8">
            <div class="head_img">
              <img :src="item.topic_pic" alt="" />
            </div>
          </el-col>
          <!-- 内容 -->
          <el-col :span="12">
            <div class="topic_content">
              <p v-html="item.topic_content"></p>
            </div>
          </el-col>

          <el-col :span="4">
            <el-link :underline="false">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="editTopic(item.topic_id)"
                >修改</el-button
              >
            </el-link>
            <el-link :underline="false">
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="deleteTopic(item.topic_id)"
                >删除</el-button
              >
            </el-link>
          </el-col>
        </el-row>
        <el-row>
          <div class="ico_list"></div>
        </el-row>
      </div>
    </div>
    <el-row v-else>
      <div style="text-align: center">您尚未发布过任何话题...</div>
    </el-row>
  </div>
</template>

<script>
export default {
  data () {
    return {
      user_id: JSON.parse(sessionStorage.getItem('user')).user_id,
      topics: []
    }
  },
  created () {
    this.getTopic()
  },
  methods: {
    // 获取话题
    async getTopic () {
      const { data: res } = await this.$http.post('/self_topic', { id: this.user_id })
      if (res.status !== 200) return
      this.topics = res.topic_info
    },
    editTopic (topicId) {
      this.$router.push('/issue?id=' + topicId)
    },
    async deleteTopic (topicId) {
      const { data: res } = await this.$http.post('/delete_topic', { topicId: topicId })
      if (res.status !== 201) return this.$message.info('删除失败！')
      this.$message.success('删除成功！')
      this.getTopic()
    }
  }
}
</script>

<style lang="less" scoped>
.head_img {
  width: 200px;
  height: 105px;
  overflow: hidden;
}
img {
  width: 100%;
  height: 100%;
}

.topic_content {
  height: 105px;
  overflow: hidden;
}

.el-link {
  margin-top: 15px;
  margin-left: 15px;
}

.ico_list {
  padding-bottom: 15px;
  margin-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
}

p {
  margin: 0;
  margin-left: 15px;
}

a {
  color: #303133;
}

/deep/ h3 {
  margin: 0;
}
</style>
